package org.jeecg.modules.system.util;

import com.alibaba.fastjson.JSONObject;
import org.jeecg.common.util.RestUtil;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.UUID;

@Component
public class RMSUtil {

    @Autowired
    private Environment environment;

    /**
     * 搬运货架到标准工作站
     *
     * @param stationId 工作站id
     * @param shelfCode 货架编码
     * @return
     */
    public JSONObject deliverShelfToStation(Integer stationId, String shelfCode, List<String> face) {
        JSONObject request = new JSONObject();// 创建请求
        request.put("id", "wms_001");
        request.put("msgType", "RobotTaskRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();// 创建请求体
        body.put("taskType", "DELIVER_SHELF_TO_STATION");
        body.put("instruction", "GO_FETCH");
        body.put("stationId", stationId);
        body.put("shelfCode", shelfCode);
        body.put("neededSides", face);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post("http://" + environment.getProperty("rms.url"), request);// 发送请求
    }

    /**
     * 货架放行
     *
     * @param taskId 任务id
     * @return
     */
    public JSONObject deliverShelfToPlacement(Integer taskId) {
        JSONObject request = new JSONObject();// 创建请求
        request.put("id", "wms_001");
        request.put("msgType", "RobotTaskUpdateRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();// 创建请求体
        body.put("taskId", taskId);
        body.put("instruction", "GO_RETURN");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post("http://" + environment.getProperty("rms.url"), request);// 发送请求
    }

    /**
     * 货架放行(单点工位)
     *
     * @param taskId       任务id
     * @param destCellCode 老家位置
     * @return
     */
    public JSONObject deliverShelfToPlacement(Integer taskId, String destCellCode) {
        JSONObject request = new JSONObject();// 创建请求
        request.put("id", "wms_001");
        request.put("msgType", "RobotTaskUpdateRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();// 创建请求体
        body.put("taskId", taskId);
        body.put("instruction", "GO_RETURN");
        body.put("destCellCode", destCellCode);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post("http://" + environment.getProperty("rms.url"), request);// 发送请求
    }

    /**
     * 3.5.4 任务更新
     * 货架转面
     *
     * @param taskId 任务id
     * @return
     */
    public JSONObject deliverShelfToTurn(Integer taskId, List<String> list) {
        JSONObject request = new JSONObject();// 创建请求
        request.put("id", "wms_001");
        request.put("msgType", "RobotTaskUpdateRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();// 创建请求体
        body.put("taskId", taskId);
        body.put("instruction", "GO_TURN");
        body.put("neededSides", list);


        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post("http://" + environment.getProperty("rms.url"), request);// 发送请求
    }

    /**
     * 搬运货架到点位
     *
     * @param shelfCode    货架编码
     * @param destCellCode 目标单元格编码
     * @return
     */
    public JSONObject deliverShelf(String shelfCode, String destCellCode) {
        JSONObject request = new JSONObject();// 创建请求
        request.put("id", "wms_001");
        request.put("msgType", "RobotTaskRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();// 创建请求体
        body.put("taskType", "DELIVER_SHELF");
        body.put("shelfCode", shelfCode);
        body.put("instruction", "GO_FETCH");
        body.put("destCellCode", destCellCode);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post("http://" + environment.getProperty("rms.url"), request);// 发送请求
    }

    /**
     * 搬运货架到单点工作站
     *
     * @param shelfCode 货架编码
     * @param stationId 工作站id
     * @return
     */
    public JSONObject deliverShelf(String shelfCode, int stationId) {
        JSONObject request = new JSONObject();// 创建请求
        request.put("id", "wms_001");
        request.put("msgType", "RobotTaskRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();// 创建请求体
        body.put("taskType", "DELIVER_SHELF");
        body.put("shelfCode", shelfCode);
        body.put("instruction", "GO_FETCH");
        body.put("stationId", stationId);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post("http://" + environment.getProperty("rms.url"), request);// 发送请求
    }

    /**
     * 查询所有单元格信息
     *
     * @return
     */
    public JSONObject cellPoint() {
        JSONObject request = new JSONObject();// 创建请求
        request.put("id", "wms_001");
        request.put("msgType", "QueryInstructionRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();// 创建请求体
        body.put("instruction", "CELL");
        body.put("queryType", "simple");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post("http://" + environment.getProperty("rms.url"), request);// 发送请求
    }

    /**
     * 查询单个单元格信息
     *
     * @return
     */
    public JSONObject cellPoint(String cellCode) {
        JSONObject request = new JSONObject();// 创建请求
        request.put("id", "wms_001");
        request.put("msgType", "QueryInstructionRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();// 创建请求体
        body.put("instruction", "CELL");
        body.put("queryType", "detail");
        body.put("cellCode", cellCode);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post("http://" + environment.getProperty("rms.url"), request);// 发送请求
    }

    /**
     * 查询单个工作站信息
     *
     * @return
     */
    public JSONObject cellStationPoint(String cellCode) {
        JSONObject request = new JSONObject();// 创建请求
        request.put("id", "wms_001");
        request.put("msgType", "QueryInstructionRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();// 创建请求体
        body.put("instruction", "STATION");
        body.put("queryType", "detail");
        body.put("stationId", cellCode);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post("http://" + environment.getProperty("rms.url"), request);// 发送请求
    }


    /**
     * 6.3.2 货架信息
     * 刷新当前货架时使用(查询当前工作站点位上面的货架编码是什么)
     *
     * @param stationPointList 工作站点位(节点编码)
     * @return
     */
    public JSONObject selectShelfCodeByStationPoint(List<String> stationPointList) {
        JSONObject request = new JSONObject();
        request.put("id", "wms_001");
        request.put("msgType", "QueryInstructionRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();
        body.put("instruction", "SHELF");
        body.put("queryType", "detail");
        body.put("locationCellCodes", stationPointList);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post("http://" + environment.getProperty("rms.url"), request);
    }

    /**
     * 6.3.7 机器人信息
     * 刷新当前货架时使用(查询当前这个工作站点位是哪个任务id)
     *
     * @param robotId 机器人id，通过6.3.2获取
     */
    public JSONObject selectTaskIdByRobotId(String robotId) {
        JSONObject request = new JSONObject();
        request.put("id", "wms_001");
        request.put("msgType", "QueryInstructionRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();
        body.put("instruction", "ROBOT");
        body.put("queryType", "detail");
        body.put("robotId", robotId);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post("http://" + environment.getProperty("rms.url"), request);
    }

    /**
     * 查询所有工作站信息
     *
     * @return
     */
    public JSONObject cellStationPoint() {
        JSONObject request = new JSONObject();// 创建请求
        request.put("id", "wms_001");
        request.put("msgType", "QueryInstructionRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();// 创建请求体
        body.put("instruction", "STATION");
        body.put("queryType", "simple");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post("http://" + environment.getProperty("rms.url"), request);// 发送请求
    }

    /**
     * 货架入场
     *
     * @param shelfCode         货架编码
     * @param locationCellCode  货架入场单元格编码
     * @param placementCellCode 货架老家单元格编码
     * @return
     */
    public JSONObject enterShelf(String shelfCode, String locationCellCode, String placementCellCode) {
        JSONObject request = new JSONObject();// 创建请求
        request.put("id", "wms_001");
        request.put("msgType", "WarehouseInstructionRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();// 创建请求体
        body.put("shelfCode", shelfCode);
        body.put("instruction", "ENTER_SHELF");
        body.put("locationCellCode", locationCellCode);
        body.put("placementCellCode", placementCellCode);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post("http://" + environment.getProperty("rms.url"), request);// 发送请求
    }

    /**
     * 移除货架 货架编码
     *
     * @return
     */
    public JSONObject removeShelf(String shelfCode) {
        JSONObject request = new JSONObject();// 创建请求
        request.put("id", "wms_001");
        request.put("msgType", "WarehouseInstructionRequestMsg");

        JSONObject httpHeaders = getHeaders();

        JSONObject body = new JSONObject();// 创建请求体
        body.put("shelfCode", shelfCode);
        body.put("instruction", "REMOVE_SHELF");

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("header", httpHeaders);
        jsonObject.put("body", body);

        request.put("request", jsonObject);

        return RestUtil.post("http://" + environment.getProperty("rms.url"), request);// 发送请求
    }

    //获取头信息
    @NotNull
    private JSONObject getHeaders() {
        JSONObject httpHeaders = new JSONObject();// 获取头信息
        httpHeaders.put("requestId", UUID.randomUUID());
        httpHeaders.put("clientCode", "wmsCode");
        httpHeaders.put("warehouseCode", "wmsWarehouseCode");
        httpHeaders.put("userId", environment.getProperty("rms.username"));
        httpHeaders.put("userKey", environment.getProperty("rms.password"));
        httpHeaders.put("language", "en_us");
        httpHeaders.put("version", "3.3.0");
        return httpHeaders;
    }
}
